package baptizeattribute.dao;

import baptizeattribute.pojo.Item;
import baptizeattribute.resource.PathConfiguration;
import baptizeattribute.utils.IOUtil;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;

public class ItemDao {
    String path = PathConfiguration.Items;
    String absPath = IOUtil.AbsolutePath;

    /**
     * 获得硬盘中的item道具
     * @return
     */
    public Item getItem(){
        Item item = new Item();
        File file = new File(absPath + path +PathConfiguration.ItemYml);
        FileConfiguration filec = YamlConfiguration.loadConfiguration(file);
        item.setItem(filec.getItemStack("Item"));
        return item;
    }

    /**
     * 将道具保存进硬盘
     * @param item
     */
    public void setItem(Item item){
        File file = new File(absPath + path +PathConfiguration.ItemYml);
        FileConfiguration filec = YamlConfiguration.loadConfiguration(file);
        filec.set("Item", item.getItem());
        try {
            filec.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 服务器第一次加载
     */
    public void setItem(){
        File file = new File(absPath + path +PathConfiguration.ItemYml);
        FileConfiguration filec = YamlConfiguration.loadConfiguration(file);
        filec.set("Item", Item.getNewItem());
        try {
            filec.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
